package com.smartleanx.module.hrm.controller.admin.esign.task;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.module.hrm.service.esign.eums.ActorTypeEnum;
import com.smartleanx.module.hrm.service.esign.eums.FolderTypeEnum;
import com.smartleanx.module.hrm.service.esign.task.ESignTaskService;
import com.smartleanx.module.hrm.service.esign.task.HrmESignRegionParam;
import com.smartleanx.module.hrm.service.esign.temp.ESignDocTempService;
import com.smartleanx.module.hrm.service.esign.temp.param.ComponentBathParam;
import com.smartleanx.module.hrm.service.esign.temp.param.HrmEContractTaskParam;
import com.smartleanx.module.hrm.service.esign.temp.param.HrmEContractTempFileParam;
import com.smartleanx.module.hrm.service.esign.temp.param.HrmEContractTempParam;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEFolder;
import com.smartleanx.module.hrm.service.esign.temp.result.*;
import com.smartleanx.module.hrm.service.esign.temp.vo.*;
import com.smartleanx.module.hrm.util.esign.EsignHttpHelper;
import com.smartleanx.module.hrm.util.esign.EsignHttpResponse;
import com.smartleanx.module.hrm.util.esign.FileUtil;
import com.smartleanx.module.hrm.util.esign.enums.EsignRequestType;
import com.smartleanx.module.hrm.util.esign.exception.EsignDemoException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name =  "模板")
@RestController
@RequestMapping("/hrm/esign/task")
public class ESignTaskController {

    @Autowired
    private ESignTaskService eSignTaskService;

    private final Boolean debug = true;

    @Operation(summary = "创建签署任务")
    @PostMapping("/createSignFlow")
    public CommonResult<Long> createSignFlow(@RequestBody SignFlowParamVo signFlowParamVo) {
        return CommonResult.success(eSignTaskService.createSignFlow(signFlowParamVo)).setCode(200);
    }

    @Operation(summary = "批量创建签署任务")
    @PostMapping("/createSignFlowBath")
    public CommonResult<ComponentBathResult> createSignFlowBath(@RequestBody SignFlowParamVo signFlowParamVo) {
        return CommonResult.success(eSignTaskService.createSignFlowBath(signFlowParamVo)).setCode(200);
    }

    @Operation(summary = "获取企业填写")
    @GetMapping("/getFillInData")
    public CommonResult<ComponentBathResult> getFillInData(Long taskId) {
        return CommonResult.success(eSignTaskService.getFillInData(taskId)).setCode(200);
    }

    @Operation(summary = "获取填写控件")
    @GetMapping("/getFillInComponents")
    public CommonResult<ComponentResult> getFillInComponents(Long taskId, String fillInType) {
        return CommonResult.success(eSignTaskService.getFillInComponents(taskId,fillInType)).setCode(200);
    }

    @Operation(summary = "做文件预览")
    @PostMapping("/getFillInViewToPDF")
    public CommonResult<String> getFillInComponents(@RequestBody HashMap<String,Object> map) {
        return CommonResult.success(eSignTaskService.getFillInViewToPDF(map)).setCode(200);
    }

    @Operation(summary = "做文件预览")
    @GetMapping("/getFillInViewDetail")
    public CommonResult<String> getFillInViewDetail(Long taskId) {
        return CommonResult.success(eSignTaskService.getFillInViewDetail(taskId)).setCode(200);
    }

    @Operation(summary = "提交填写")
    @PostMapping("/saveFillInData")
    public CommonResult<String> saveFillInData(@RequestBody FillInViewPDFLinkVo fillInViewPDFLinkVo) {
        eSignTaskService.saveFillInData(fillInViewPDFLinkVo,null,null);
        return CommonResult.success("成功").setCode(200);
    }

    @Operation(summary = "批量提交填写")
    @PostMapping("/saveFillInDataBath")
    public CommonResult<String> saveFillInDataBath(@RequestBody ComponentBathResult param) {
        param.setFillInType(1);
        eSignTaskService.saveFillInDataBath(param);
        return CommonResult.success("成功").setCode(200);
    }

    @Operation(summary = "获取任务列表")
    @GetMapping("/getTaskPage")
    public CommonResult<PageResult<HrmETaskListResult>> getTaskPage(HrmEContractTaskParam param) {
        return CommonResult.success(eSignTaskService.getTaskPage(param)).setCode(200);
    }

    @Operation(summary = "获取签署链接")
    @GetMapping("/getSignFlowLink")
    public CommonResult<String> getSignFlowLink(Long taskId,String actorType,String redirectUrl) {
        actorType = ActorTypeEnum.CORP.getCode();
        return CommonResult.success(eSignTaskService.getSignFlowLink(taskId,actorType,redirectUrl)).setCode(200);
    }

    @Operation(summary = "获取合同编号")
    @GetMapping("/getContractCode")
    public CommonResult getContractCode(Long tempId) {
        return CommonResult.success(eSignTaskService.getContractCode(tempId,null)).setCode(200);
    }

    @Operation(summary = "撤回签署")
    @PostMapping("/revokeTask")
    public CommonResult revokeTask(@RequestBody JSONObject jsonObject) {
        Long taskId = jsonObject.getLong("taskId");
        String revokeReason = jsonObject.getStr("revokeReason");
        return CommonResult.success(eSignTaskService.revokeTask(taskId,revokeReason)).setCode(200);
    }

    @Operation(summary = "删除签署")
    @PostMapping("/removeTask/{taskId}")
    public CommonResult removeTask(@PathVariable("taskId") Long taskId) {
        return CommonResult.success(eSignTaskService.removeTask(taskId)).setCode(200);
    }

    @Operation(summary = "催办签署")
    @PostMapping("/urgeTask/{taskId}")
    public CommonResult urgeTask(@PathVariable("taskId") Long taskId) {
        return CommonResult.success(eSignTaskService.urgeTask(taskId)).setCode(200);
    }

    @Operation(summary = "获取任务编辑信息")
    @GetMapping("/getTaskDetail")
    public CommonResult getTaskDetail(Long taskId) {
        return CommonResult.success(eSignTaskService.getTaskDetail(taskId)).setCode(200);
    }

    @Operation(summary = "发起任务时获取模板名称列表")
    @GetMapping("/getTempNameList")
    public CommonResult getTempNameList(String tempName) {
        return CommonResult.success(eSignTaskService.getTempNameList(tempName)).setCode(200);
    }


    @Operation(summary = "发起任务时获取模板信息")
    @GetMapping("/getTempDetailToTask")
    public CommonResult getTempDetailToTask(Long tempId) {
        return CommonResult.success(eSignTaskService.getTempDetailToTask(tempId)).setCode(200);
    }

    @Operation(summary = "提交填写")
    @PostMapping("/saveFillInDataApp")
    public CommonResult<String> saveFillInDataApp(@RequestBody FillInViewPDFLinkVo fillInViewPDFLinkVo) {
        fillInViewPDFLinkVo.setSaveType(2);
        fillInViewPDFLinkVo.setActorType("2");
        eSignTaskService.saveFillInData(fillInViewPDFLinkVo,null,null);
        return CommonResult.success("成功");
    }

    @Operation(summary = "获取轮询状态")
    @GetMapping("/getTaskSignStatus")
    public CommonResult getTaskSignStatus(Long taskId) {
        return CommonResult.success(eSignTaskService.getTaskSignStatus(taskId,ActorTypeEnum.CORP.getCode())).setCode(200);
    }

    @Operation(summary = "修改签署区")
    @PostMapping("/updateSignRegion")
    public CommonResult updateSignRegion(@RequestBody HrmESignRegionParam param) {
        return CommonResult.success(eSignTaskService.updateSignRegion(param)).setCode(200);
    }

    @Operation(summary = "计算合同到期时间和状态")
    @PostMapping("/getContractTimeAndStatus")
    public CommonResult getContractTimeAndStatus(@RequestBody ComponentBathParam param) {
        return CommonResult.success(eSignTaskService.getContractTimeAndStatus(param)).setCode(200);
    }
}
